﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using Daycare.Models;

namespace Daycare.DAL
{
    public class ConfigurationSettingsRepository
    {
        public List<ConfigurationSetting> GetConfigurationSettings(int groupId = 0)
        {
            DataTable tempTable = new DataTable();
            List<ConfigurationSetting> configs = new List<ConfigurationSetting>();

            try
            {
                using (SqlConnection con = new SqlConnection(Configuration.ConnectionString))
                {
                    SqlDataAdapter sda = new SqlDataAdapter();
                    sda.SelectCommand = new SqlCommand("daycare.GetConfigurationSettings", con);
                    sda.SelectCommand.CommandType = CommandType.StoredProcedure;

                    if (groupId > 0)
                    {
                        sda.SelectCommand.Parameters.AddWithValue("@ConfigurationSettingsGroupId", groupId);
                    }

                    sda.Fill(tempTable);
                }
            }
            catch (SqlException)
            {
                return configs;
            }

            foreach (DataRow row in tempTable.Rows)
            {
                ConfigurationSetting config = new ConfigurationSetting(Int32.Parse(row["ConfigurationSettingsId"].ToString()), Int32.Parse(row["ConfigurationSettingsGroupId"].ToString()), row["ConfigurationSettingsName"].ToString(), row["ConfigurationSettingsDescription"].ToString(), row["ConfigurationSettingsGroupName"].ToString());
                configs.Add(config);
            }

            return configs;
        }
    }
}